define([
    'jquery',
    'underscore',
    'backbone',
    'module/config/models/ConfigModel',
    'module/config/models/ConfigCollection',
    'module/config/views/List',
    'text!module/config/templates/main.html',
    'text!module/config/templates/add.html',
    'text!module/config/templates/edit.html',
    'text!module/config/templates/drop.html',
], function($, _, Backbone, ConfigModel, ConfigCollection, List, main, add, edit, drop) {

    var ConfigController = Backbone.View.extend({
        el: $("#page"),
        initialize: function() {

        },
        events: {
            "click #edit_action": "edit_action",
        },
        render: function() {
            this.$el.html(main);
            $("#main-list").html(edit);
            var config = new ConfigModel({id: 1});
            config.fetch({
                success: function(data) {
                    data = data.toJSON();
                    $("#company_name").val(data.company_name);
                    $("#company_ruc").val(data.company_ruc);
                    $("#company_address").val(data.company_address);
                    $("#tax").val(data.tax);
                    $("#current_year option[value="+data.current_year+"]").attr('selected', 'selected');
                    $("#current_month option[value="+data.current_month+"]").attr('selected', 'selected');                    
                }
            });
        },
        edit_action: function() {
            var self = this;
            var config = new ConfigModel();
            var configDetails = {
                id: 1,
                name: $("#name").val(),
                company_name: $("#company_name").val(),
                company_ruc: $("#company_ruc").val(),
                company_address: $("#company_address").val(),
                tax: $("#tax").val(),
                current_year: $("#current_year option:selected").val(),
                current_month: $("#current_month option:selected").val()

            };
            config.save(configDetails, {
                success: function(data) {
                    alert("Actualizacion satifactoria");
                }
            });
        }
    });
    return ConfigController;
});
